package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.User;
import com.kgc.scd.mapper.UserMapper;
import com.kgc.scd.service.LoginRegiservice;
import com.kgc.scd.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @program: com.kgc.scd.service.impl
 * @author: HarryXzy
 * @create: 2022-06-09 23:04
 * @description:
 **/
@Service
public class LoginRegiserviceImpl implements LoginRegiservice {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public User haveAcct(String openid) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_wxname",openid);
        User user = userMapper.selectOne(userQueryWrapper);
        return user;
    }

    @Override
    public String getTokenByWX(User user) {
        // 用户名和密码正确，生成登录用户token令牌，并返回（规则可以是uuid，或者用户编号md5加密，或者其它方式）
        String token = "T" + UUID.randomUUID().toString().replace("-", "");
        // 将token存入redis(将token作为key，用户详情作为value，存入redis)，增加过期时间1200分钟
        redisUtils.set(token, user, 7200);
        return token;
    }

    @Override
    public User havePhone(String phoneNO) {
        // 判断绑定过的手机号不能再次绑定
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_phone",phoneNO);
        User user = userMapper.selectOne(userQueryWrapper);
        return user;
    }

    @Override
    public boolean selectWxid(String wxId) {
        // 判断该微信id是否已经绑定过手机
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_wxname",wxId);
        User user = userMapper.selectOne(userQueryWrapper);
        return user != null;
    }

    @Override
    public Boolean saveUser(User user) {
        int insert = userMapper.insert(user);
        return insert > 0;
    }

    @Override
    public User getUserByPhone(String phoneNo) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_phone",phoneNo);
        User user = userMapper.selectOne(userQueryWrapper);
        return user;
    }

    @Override
    public User loginByUsernameOrPhone(String userNameOrPhone, String userPwd) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_pwd",userPwd);
        userQueryWrapper.and(wrapper -> wrapper.eq("user_name",userNameOrPhone).or().eq("user_phone", userNameOrPhone));
        User user= userMapper.selectOne(userQueryWrapper);
        return user;
    }

    @Override
    public boolean haveUserName(String userName) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_name",userName);
        User user = userMapper.selectOne(userQueryWrapper);
        return user != null;
    }


}
